package com.qzdatasoft.mcp.server.demo.resource;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qzdatasoft.mcp.server.common.AbstractResourceProvider;
import com.qzdatasoft.mcp.server.common.ProviderType;
import io.modelcontextprotocol.server.McpSyncServerExchange;
import io.modelcontextprotocol.spec.McpSchema;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;

@Service
public class SystemInfoResourceProvider extends AbstractResourceProvider {

    @Override
    protected McpSchema.Resource getMeta() {
        return new McpSchema.Resource(
                "system://info",
                "System Information",
                "Provides basic system information including Java version, OS, etc.",
                "application/json", null
        );
    }

    @Override
    protected BiFunction<McpSyncServerExchange, McpSchema.ReadResourceRequest, McpSchema.ReadResourceResult> handler() {
        return (exchange, request) -> {
            try {
                String jsonContent = new ObjectMapper().writeValueAsString(Map.of(
                        "javaVersion", System.getProperty("java.version"),
                        "osName", System.getProperty("os.name"),
                        "osVersion", System.getProperty("os.version"),
                        "osArch", System.getProperty("os.arch"),
                        "processors", Runtime.getRuntime().availableProcessors(),
                        "timestamp", System.currentTimeMillis()
                ));
                return new McpSchema.ReadResourceResult(
                        List.of(new McpSchema.TextResourceContents(request.uri(), "application/json", jsonContent)));
            } catch (Exception e) {
                throw new RuntimeException("Failed to generate system info", e);
            }
        };
    }

    @Override
    public ProviderType getType() {
        return ProviderType.RESOURCE;
    }
}
